﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class PolicyBiz
    {
        /// <summary>
        /// 获取所有政策信息
        /// </summary>
        /// <returns></returns>
        public static List<Policy> GetPolicyList()
        {
            try
            {
                var policyList = new Policy().FindAll();
                return policyList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 分页获取政策信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Policy> GetpolicyListByPage(int page, int pageSize, string key, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var policyList = (from c in ctx.Policy where c.Status != (int)ItemStatus.Delete && c.PolicyName.Contains(key) orderby c.CreateTime descending select c); // 被删除（status为2）的记录不展示
                    count = policyList.Count();
                    var data = policyList
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Policy().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 根据状态获取特殊策略
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<Policy> GetSpePolicyByStatus(int status)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var policyList = (from c in ctx.Policy where c.Status == status && c.IsAttach orderby c.ID select c);
                    var data = policyList.ToList().Select(p => new Policy().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 增加政策
        /// </summary>
        /// <param name="policyName"></param>
        /// <param name="durationType"></param>
        /// <param name="balanceType"></param>
        /// <param name="rate"></param>
        /// <param name="isAttach"></param>
        /// <param name="settlementType"></param>
        /// <param name="duration"></param>
        /// <returns></returns>
        public static bool PolicyAdd(string policyName, int balanceType, decimal rate, int settlementType, int duration)
        {
            try
            {
                var policy = new Policy()
                {
                    CreateUser = System.Web.HttpContext.Current.User.Identity.Name,
                    BalanceType = (BalanceType)balanceType,
                    CreateTime = DateTime.Now,
                    Duration = duration,
                    DurationType = DurationType.ByMonth,
                    SettlementType = (SettlementType)settlementType,
                    IsAttach = true,
                    PolicyName = policyName,
                    Rate = rate,
                    Status = ItemStatus.Enable,
                    CycleAmount = 0
                };
                return policy.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID获取政策信息
        /// </summary>
        /// <param name="policyId"></param>
        /// <returns></returns>
        public static Policy GetPolicyByID(int policyId)
        {
            try
            {
                return new Policy().Find(policyId.ToString());
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 修改政策
        /// </summary>
        /// <param name="policyId"></param>
        /// <param name="policyName"></param>
        /// <param name="cycle"></param>
        /// <param name="balanceType"></param>
        /// <param name="rate"></param>
        /// <param name="isAttach"></param>
        /// <param name="status"></param>
        /// <param name="createUser"></param>
        /// <returns></returns>
        public static bool PolicyEdit(int policyId, string policyName, int balanceType, decimal rate, int settlementType, int duration)
        {
            try
            {
                var policy = new Policy().Find(policyId.ToString());
                if (policy != null)
                {
                    policy.CreateUser = System.Web.HttpContext.Current.User.Identity.Name;
                    policy.BalanceType = (BalanceType)balanceType;
                    policy.CreateTime = DateTime.Now;
                    policy.SettlementType = (SettlementType)settlementType;
                    policy.DurationType = DurationType.ByMonth;
                    policy.Duration = duration;
                    policy.IsAttach = true;
                    policy.PolicyName = policyName;
                    policy.Rate = rate;
                    policy.CycleAmount = 0;
                    return policy.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }

        /// <summary>
        /// 改变政策状态
        /// </summary>
        /// <param name="policyId"></param>
        /// <param name="destinationStatus"></param>
        /// <returns></returns>
        public static bool PolicyStatusChange(int policyId, int destinationStatus)
        {
            try
            {
                var policy = new Policy().Find(policyId.ToString());
                if (policy != null)
                {
                    policy.Status = (ItemStatus)destinationStatus;
                    return policy.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }
    }
}
